package com.mall.goods.service.impl;

import com.mall.goods.mapper.CategoryMapper;
import com.mall.goods.model.Category;
import com.mall.goods.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-08-18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addCategory(Category category){
        return super.save(category);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(Integer id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateCategory(Category category){
        return super.updateById(category);
    }

    @Override
    public Category getById(Integer id){
        return super.getById(id);
    }

    @Override
    public List<Category> selectList(Category category){
        return super.list(getQueryWrapper(category));
    }

    @Override
    public Page<Category> selectPage(Page<Category> page, Category category){
        return super.page(page, getQueryWrapper(category));
    }

    public LambdaQueryWrapper<Category> getQueryWrapper(Category category){
        return Wrappers.<Category>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(category.getId()), Category::getId, category.getId())
            .eq(ObjectUtils.isNotEmpty(category.getName()), Category::getName, category.getName())
            .eq(ObjectUtils.isNotEmpty(category.getSort()), Category::getSort, category.getSort())
            .eq(ObjectUtils.isNotEmpty(category.getParentId()), Category::getParentId, category.getParentId())
        ;
    }
}
